import functools
from typing import Callable
from loguru import logger

# 配置日志
logger.add(
    "logs/app.log",
    rotation="500 MB",
    retention="10 days",
    level="DEBUG",
    encoding="utf-8"
)

logger.add(
    "logs/error.log",
    rotation="500 MB",
    retention="10 days",
    level="ERROR",
    encoding="utf-8"
)

def log_exception(func: Callable) -> Callable:
    """异常日志装饰器"""
    @functools.wraps(func)
    async def wrapper(*args, **kwargs):
        try:
            return await func(*args, **kwargs)
        except Exception as e:
            # 获取函数信息
            module = func.__module__
            name = func.__name__
            # 记录异常日志
            logger.exception(f"Exception in {module}.{name}: {str(e)}")
            raise
    return wrapper
